package dao;

import hibernate.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import dto.RutaDTO;
import entities.EnvioEntity;
import entities.RutaEntity;

public class RutaDAO {
	private static RutaDAO instancia = null;
	private static SessionFactory sf = null;
	
	private RutaDAO(){}
	
	public static RutaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new RutaDAO();
		} 
		return instancia;
	}
	
	public void grabarRuta(RutaEntity ruta){
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(ruta);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void actualizarRuta(RutaEntity ruta){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(ruta);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public RutaEntity LoadRuta(int id) 
	{
		Session session = sf.openSession();
		session.beginTransaction();
		RutaEntity rut = (RutaEntity) session.get(RutaEntity.class, id);
		session.flush();
		session.getTransaction().commit();
		session.close();
		return rut;
	}
}